#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

int minimumMoves(vector<vector<int>>& grid) {
    vector<pair<int, int>> more, less;
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            if (grid[i][j] > 1)
                for (int k = 2; k <= grid[i][j]; k++)
                    more.push_back({ i,j });
            else if (grid[i][j] == 0)
                less.push_back({ i,j });

    int res = INT_MAX;
    do
    {
        int steps = 0;
        for (int i = 0; i < more.size(); i++)
            steps += abs(more[i].first - less[i].first) + abs(more[i].second - less[i].second);
        res = min(res, steps);
    } while (next_permutation(more.begin(), more.end()));
    return res;
}

int main()
{
    vector<vector<int>> vv = { {1,2,2},{1,1,0},{0,1,1} };
    minimumMoves(vv);
    return 0;
}